Also, if any matches were found, grep exits with a status of 0, but if no matches were found it exits with a status of 1. This is useful for building shell scripts that use grep as a condition for, for example, the if statement.
When invoked as
egrep
the syntax of the
expr
is slightly different; See below.
(grep) (egrep) (explanation)
c c a single (non-meta) character matches itself.
. . matches any single character except newline.
\? ? postfix operator; preceeding item is optional.
* * postfix operator; preceeding item 0 or
more times.
\+ + postfix operator; preceeding item 1 or
more times.
\| | infix operator; matches either
argument.
^ ^ matches the empty string at the beginning of a line.
$ $ matches the empty string at the end of a line.
\< \< matches the empty string at the beginning of a word.
\> \> matches the empty string at the end of a word.
[chars] [chars] match any character in the given class; if the
first character after [ is ^, match any character
not in the given class; a range of characters may
be specified by first-last; for example, \W
(below) is equivalent to the class [^A-Za-z0-9]
\( \) ( ) parentheses are used to override operator precedence.
\digit \digit \n matches a repeat of the text
matched earlier in the regexp by the subexpression inside the nth
opening parenthesis.
\ \ any special character may be preceded
by a backslash to match it literally.
(the following are for compatibility with GNU Emacs)
\b \b matches the empty string at the edge of a word.
\B \B matches the empty string if not at the edge of a word.
\w \w matches word-constituent characters (letters & digits).
\W \W matches characters that are not word-constituent.
Operator precedence is (highest to lowest) ?, *, and +, concatenation, and finally |. All other constructs are syntactically identical to normal characters. For the truly interested, the file dfa.c describes (and implements) the exact grammar understood by the parser.
-b prints a byte offset instead of a block offset.
The {m,n} construct of System V grep is not implemented.
James A. Woods is responsible for the hybridized search strategy of using Boyer-Moore-Gosper fixed-string search as a filter before calling the general regexp matcher.
Arthur David Olson contributed code that finds fixed strings for the aforementioned BMG search for a large class of regexps.
Richard Stallman wrote the backtracking regexp matcher that is used for \digit backreferences, as well as the GNU getopt. The backtracking matcher was originally written for GNU Emacs.
D. A. Gwyn wrote the C alloca emulation that is provided so System V machines can run this program. (Alloca is used only by RMS' backtracking matcher, and then only rarely, so there is no loss if your machine doesn't have a "real" alloca.)
Scott Anderson and Henry Spencer designed the regression tests used in the "regress" script.
Paul Placeway wrote the original version of this manual page.